package cn.com.dingtek.genius.manager.server.web.advice;

import cn.com.dingtek.genius.manager.server.support.sam.UnauthenticatedException;
import cn.com.dingtek.genius.manager.server.support.sam.UnenforcerException;
import cn.com.dingtek.genius.manager.server.utils.VerifyException;
import cn.com.dingtek.genius.manager.server.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class ResponseExceptionControllerAdvice {
    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(UnauthenticatedException e) {
        return new Result(ResponseCode.INVALID_AUTH.getCode(), ResponseCode.INVALID_AUTH.getMsg());
    }

    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(UnenforcerException e) {
        return new Result(ResponseCode.INVALID_PERMISSION.getCode(), ResponseCode.INVALID_PERMISSION.getMsg());
    }

    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(VerifyException e) {
        return new Result(ResponseCode.VERIFY_ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        log.error("System Exception", e);
        return new Result(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getMsg());
    }
}
